'''
Created on Jul 28, 2011

@author: mark
'''
from data import get_cursor

class Partner(object):
    '''
    classdocs
    '''

    def __init__(self, p1, p2, id=None):
        '''
        Constructor
        '''
        self.p1, self.p2 = p1, p2
        if id is None:
            curs = get_cursor()
            curs.execute('INSERT INTO partners(p1, p2) VALUES (%d, %d)' % (self.p1.id, self.p2.id))
            curs.connection.commit()
            curs.execute('SELECT LAST_INSERT_ID()')
            self.id = curs.fetchone()[0]
            
    def save(self):
        curs = get_cursor()
        curs.execute('UPDATE partners SET p1=%d, p2=%d WHERE id=%d'
                     % (self.p1, self.p2, self.id))
        curs.connection.commit()
        
    def __str__(self):
        return '(%s, %s (%d))' % (self.p1.name, self.p2.name, self.id)